﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace indicadores.Models
{
    public class LoginRepositorio
    {
        IndicadoresContext context = new IndicadoresContext();

        public void Adiciona(Login l)
        {
            //try
            //{
                context.Logins.Add(l);
                context.SaveChanges();
            //}
            //catch (Exception e)
            //{
              //  new Logs(e.ToString(), "Adicionar Login");
            //
        }

        public Login Busca(int id)
        {
            try
            {
                return context.Set<Login>().Find(id);
            }
            catch (Exception e)
            {
                new Logs(e.ToString(), "Buscar Login");
                Login l = new Login();
                l.Id = 0;
                l.Nome = "Erro com o banco";
                l.Usuario = "Erro com o banco";
                l.Acesso = "Erro com o banco";
                return l;
            }
        }

        public List<Login> BuscaTodas()
        {
            try
            {
                var consulta = from e in context.Set<Login>()
                               select e;
                return consulta.ToList();
            }
            catch (Exception e)
            {
                new Logs(e.ToString(), "Buscar Todos Logins");
                return new List<Login>();
            }
        }

        public void Alterar(Login l)
        {
            try
            {
                context.Entry(l).State = EntityState.Modified;
                context.SaveChanges();
            }
            catch (Exception e)
            {
                new Logs(e.ToString(), "Alterar Login");
            }
        }

        public void Apagar(int id)
        {
            try
            {
                context.Logins.Remove(Busca(id));
                context.SaveChanges();
            }
            catch (Exception e)
            {
                new Logs(e.ToString(), "Apagar Login");
            }
        }
    }
}